<?php

/**
 * Object representation of a Chatlog table in the database.
 */
class ChatlogTable {
	/**
	 * Gets a chatlog by looking for it's primary key.
	 * 
	 * @param	userID		The user ID. 
	 * @param	protocol	Protocol of the log.
	 * @param	account		Account name.
	 * @param	recipient	Recipient of the conversation.
	 * @param	startDate	UNIX timestamp of the start of the conversation.
	 * @return The corresponding Chatlog or NULL if failed.
	 */
	public static function get($userID, $protocol, $account, $recipient, $startDate) {
		// Check input:
		if (!$userID or !$protocol or !$account or !$recipient or !$startDate) {
			return NULL;
		}
		
		// Get DB connection:
		$db = Database::getDB();
		
		// Prepare statement:
		$stmt = $db->prepare("SELECT * FROM User_" . $userID . "_Logs WHERE protocol = :protocol AND account = :account AND recipient = :recipient AND startDate = :startDate LIMIT 1");
	  	$stmt->bindValue(":protocol", $protocol);
	  	$stmt->bindValue(":account", $account);
	  	$stmt->bindValue(":recipient", $recipient);
	  	$stmt->bindValue(":startDate", $startDate);
	  	
	  	// Execute:
	  	Logger::log("ChatlogTable", "Fetching Chatlog from DB.");
	  	$stmt->execute();
	  	$row = $stmt->fetch(PDO::FETCH_ASSOC);
	  	Logger::log("ChatlogTable", "Chatlog fetched.");
	  	
	  	// Make object and return:
	  	$chatlogFactory = new ChatlogFactory();
	  	return $chatlogFactory->fromRow($row);
	}
	
	
	
	/**
	 * Inserts the given log in the database.
	 * 
	 * @param userID	ID of the user.
	 * @param log		The log to insert.
	 */
	public static function insert($userID, Chatlog $log) {
		// Check input:
		if (!$userID or !log or !$log->getProtocol() or !$log->getAccount() or !$log->getRecipient() or !$log->getStartDate()) {
			return;
		}
		
		// Get DB connection:
		$db = Database::getDB();
		
		// Prepare statement:
		$stmt = $db->prepare("INSERT INTO User_" . $userID . "_Logs (protocol, account, recipient, startDate, contents, revision) VALUES (:protocol, :account, :recipient, :startDate, :contents, :revision)");
	  	$stmt->bindValue(":protocol", $log->getProtocol());
	  	$stmt->bindValue(":account", $log->getAccount());
	  	$stmt->bindValue(":recipient", $log->getRecipient());
	  	$stmt->bindValue(":startDate", $log->getStartDate());
	  	$stmt->bindValue(":contents", $log->getContents());
	  	$stmt->bindValue(":revision", $log->getRevision());
	  	
	  	// Execute statement:
	  	Logger::log("ChatlogTable", "Inserting new log.");
	  	$stmt->execute();
	  	Logger::log("ChatlogTable", "Log inserted.");
	}
	
	
	
	/**
	 * Updates the given chatlog.
	 * 
	 * @param userID	ID of the user.
	 * @param log		The log to update.
	 */
	public static function update($userID, Chatlog $log) {
		// Check input:
		if (!$userID or !log or !$log->getProtocol() or !$log->getAccount() or !$log->getRecipient() or !$log->getStartDate()) {
			return;
		}
		
		// Get DB connection:
		$db = Database::getDB();
		
		// Prepare statement:
		$stmt = $db->prepare("UPDATE User_" . $userID . "_Logs SET contents = :contents, revision = :revision WHERE protocol = :protocol AND account = :account AND recipient = :recipient AND startDate = :startDate LIMIT 1");
	  	$stmt->bindValue(":protocol", $log->getProtocol());
	  	$stmt->bindValue(":account", $log->getAccount());
	  	$stmt->bindValue(":recipient", $log->getRecipient());
	  	$stmt->bindValue(":startDate", $log->getStartDate());
	  	$stmt->bindValue(":contents", $log->getContents());
	  	$stmt->bindValue(":revision", $log->getRevision());
	  	
	  	// Execute statement:
	  	Logger::log("ChatlogTable", "Updating log.");
	  	$stmt->execute();
	  	Logger::log("ChatlogTable", "Log updated.");
	}
	
	
	
	/**
	 * Gets all log objects of a given protocol/account.
	 * 
	 * @param userID		ID of the user.
	 * @param protocol		The protocol.
	 * @param account		The account.
	 * @param loadContents	Whether or not to load the contents for each log aswell. Defaults to FALSE.
	 * @return An array containing all corresponidng log objects.
	 */
	public static function getList($userID, $protocol, $account, $loadContents = FALSE) {
		// Check input:
		if (!$userID or !$protocol or !$account) {
			return;
		}
		
		// Get DB connection:
		$db = Database::getDB();
		
		// Build statement
		if ($loadContents) {
			$stmt = $db->prepare("SELECT * FROM User_" . $userID . "_Logs WHERE protocol = :protocol AND account = :account");
		} else {
			$stmt = $db->prepare("SELECT protocol, account, recipient, startDate, revision FROM User_" . $userID . "_Logs WHERE protocol = :protocol AND account = :account");
		}
	  	$stmt->bindValue(":protocol", $protocol);
	  	$stmt->bindValue(":account", $account);
	  	
	  	// Fetch results:
	  	$count = 0;
	  	$chatlogFactory = new ChatlogFactory();
	  	Logger::log("ChatlogTable", "Fetching array of logs.");
	  	$stmt->execute();
	  	$row = $stmt->fetch(PDO::FETCH_ASSOC);
	  	$logList = array();
	  	while ($row) {
	  		// Add to logList
	  		$log = $chatlogFactory->fromRow($row);
	  		if ($log) {
	  			$logList[] = $log;
	  		}
	  		
	  		// Next row:
	  		$row = $stmt->fetch(PDO::FETCH_ASSOC);
	  		$count++;
	  	}
	  	Logger::log("ChatlogTable", "Fetched " . $count . " logs.");
	  	
	  	// Return array of logs:
	  	return $logList;	  	
	}
}